package com.kaisheng.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaisheng.entity.Goal;
import com.kaisheng.util.DBHelp;
import com.kaisheng.util.RowMapper;

public class GoalDao {
	DBHelp<Goal> db = new DBHelp<Goal>();
	public List<Goal> findGoal(String projectId) {
		String sql = "SELECT id,`name`,`describe`,project_id,createtime,rate FROM t_goal WHERE  project_id = ?";
		return db.executeQueryForList(sql, new GoalRowMapper(),projectId);
	}
	public Goal findById(String goal_id){
		String sql = "SELECT id,`name`,`describe`,project_id,createtime,rate FROM t_goal WHERE id = ?";
		return db.executeQueryForObject(sql, new GoalRowMapper(),goal_id);
	}
	
	public boolean save(Goal goal) {
		String sql = "INSERT INTO t_goal(id,`name`,`describe`,project_id,createtime,rate) VALUES(?,?,?,?,?,?)";
		return db.executeSQL(sql, goal.getId(),goal.getName(),goal.getDescribe(),goal.getProject_id(),goal.getCreatetime(),goal.getRate());
	}
	public boolean update(String id, String name, String describe) {
		String sql = "UPDATE t_goal SET `name` = ?,`describe`= ? WHERE id = ?";
		return db.executeSQL(sql, name,describe,id);
		
	}
	public boolean uprate(float rate,String goal_id) {
		String sql = "UPDATE t_goal SET rate = ? WHERE id = ?";
		return db.executeSQL(sql, rate,goal_id);
		
	}


	
	public class GoalRowMapper implements RowMapper<Goal>{

		public Goal mapRow(ResultSet rs) throws SQLException {
			Goal goal = new Goal();
			goal.setId(rs.getString("id"));
			goal.setName(rs.getString("name"));
			goal.setDescribe(rs.getString("describe"));
			goal.setProject_id(rs.getString("project_id"));
			goal.setCreatetime(rs.getString("createtime"));
			goal.setRate(rs.getFloat("rate"));
			return goal;
		}
		
	}


	

	
	

}
